<?php
class console_receipt_purchasereturn extends console_receipt_common{

    function __construct() {
        $this->rpObj = app::get('purchase')->model('returned_purchase');
        $this->rp_itemObj = app::get('purchase')->model('returned_purchase_items');
        
    }
    private $_returnpurchase = array();

    private static $return_status = array(
        'PARTIN'=>'4',
        'FINISH'=>'2',
        'CANCEL'=>'3',
        'CLOSE'=>'3',
        'FAILED'=>'3'
        
    );

   /**
     *
     * 采购退货单更新方法
     * @param array $data 采购退货单数据信息
     * @msg 错误信息
     */
    public function update($data,&$msg){
        $purchasereturn = $this->_returnpurchase;
        $io_status = $data['io_status'];
        $rp_id = $purchasereturn['rp_id'];
        $iostock_update = true;
        $items = $data['items'];#货品明细
        
        $out_stock_data = array(
            'supplier_id'       => $purchasereturn['supplier_id'],
            'branch_id'         => $purchasereturn['branch_id'],
            'rp_bn'             => $purchasereturn['rp_bn'],
            'rp_id'             => $rp_id,
            'memo'              => $data['memo'],
            'operate_time'      => $data['operate_time'],
            'operator'          => $purchasereturn['operator'],
        );
        if ($items){
            #检查明细
            
            if (!$this->checkBnexist($rp_id,$items)){
                $msg = '不存在的货号';
                return false;
            }
            #检测库存是否不足
            if (!$this->checkStore($purchasereturn['branch_id'],$items,$msg)) {
                return false;
            }
            $out_stock_data['items'] = $this->_format_items($items);
            
        }
        kernel::database()->exec('begin');
        if (count($out_stock_data['items'])>0){#有明细时才会执行退货
            if (!$this->_save_iostockorder($out_stock_data)){
                $msg = "保存采购退货出库结果失败";
                kernel::database()->exec('rollback');
                return false;
            }
        }
        
        $po_data = array('return_status'=>self::$return_status[$io_status]);
        #备注处理
        if ($data['memo']){#有备注更新
            $po_data['memo'] = $this->format_memo($purchasereturn['memo'],$data['memo']);
        }
        
        $result = $this->rpObj->update($po_data,array('rp_id'=>$rp_id));
        if (!$result){
            $msg = '采购退货单状态更新失败';
            kernel::database()->exec('rollback');
            return false;
        }
        kernel::database()->exec('commit');
        $this->clear_stockout_store_freeze($out_stock_data,$io_status);
        return true;
    }

    private function _format_items($items){
        $purchasereturn = $this->_returnpurchase;
        $iostock_items = array();
        foreach($items as $item){
            $return_item   = $purchasereturn['items'][$item['bn']];
            $effective_num = $return_item['num']-$return_item['out_num'];
            $out_num       = $item['num'] + intval($return_item['out_num']);
            $item_data     = array('out_num'=>$out_num);

            $this->rp_itemObj->update($item_data,array('item_id'=>$return_item['item_id']));
            $products = $this->getProducts($item['bn']);
            $iostock_items[] = array(
                'bn'           => $item['bn'],
                'product_id'   => $return_item['product_id'],
                'nums'         => $item['num'],
                'price'        => $return_item['price'],
                'item_id'      => $return_item['item_id'],
                'effective_num'=> $effective_num,
                'name'         => $products['name'],
                'unit'         => $products['unit'],
            );
        }
        return $iostock_items;
    }
    /**
     *
     * 采购退款单取消
     * @param array $po_bn 采购退款单编号
     */
    public function cancel($data){
       
        $rp_bn = $data['io_bn'];
        $purchasereturnInfo = $this->_returnpurchase;
        $po_data = array('return_status'=>'3');
        if ($data['memo']){#有备注更新
            $memo = $this->format_memo($purchasereturn['memo'],$data['memo']);
            $po_data['memo'] = $memo;
        }
        
        $result = $this->rpObj->update($po_data,array('rp_bn'=>$rp_bn));
        
        $this->clear_stockout_store_freeze($purchasereturnInfo);
        return true;
      
    }

    /**
     *
     * 检查采购退款单货号是否存在
     * @param array $rp_id 
     */
    public function checkBnexist($rp_id,$items){
        $rpObj = app::get('purchase')->model('returned_purchase');
        $bn_array = array();
        foreach($items as $item){
            $bn_array[]=$item['bn'];
        }
        $bn_total = count($bn_array);
        
        $bn_array = '\''.implode('\',\'',$bn_array).'\'';
        $rp_items = $rpObj->db->selectrow('SELECT count(item_id) as count FROM sdb_purchase_returned_purchase_items WHERE rp_id='.$rp_id.' AND bn in ('.$bn_array.')');
       
        if ($bn_total!=$rp_items['count']){#比较数目是否相等
            return false;
        }
        return true;
    }

    /**
     *
     * 检查采购单是否存在判断
     * @param array $rp_bn 采购单编号
     */
    public function checkExist($rp_bn){
        $rpObj = app::get('purchase')->model('returned_purchase');
        $purchasereturn = $this->rpObj->dump(array('rp_bn'=>$rp_bn), '*', array('returned_purchase_items'=>array('*')));
        if ($purchasereturn){
            $returned_purchase_items =$purchasereturn['returned_purchase_items'];
            $items = array();
            foreach ($returned_purchase_items as $k=>$item){
                $item['nums'] = $item['num'];
                $items[$item['bn']] = $item;
            }
            unset($purchasereturn['returned_purchase_items']);
            $purchasereturn['items'] = $items;
            $this->_returnpurchase = $purchasereturn;
            
             
        }
    }

    /**
     *
     * 检查采购退款单是否有效
     * @param  $rp_bn 采购单编号
     * @param $status 需要执行状态
     * @msg 返回结果
     *
     */
    public function checkValid($rp_bn,$status,&$msg){
        $this->checkExist($rp_bn);
        if(!$this->_returnpurchase){
            $msg = '采购退货单编号不存在';
            return false;
        }
        $return_status = $this->_returnpurchase['return_status'];
        switch($status){
            case 'PARTIN':
            case 'FINISH':
                if ($return_status=='2' || $return_status=='3' || $return_status=='5'){
                    $msg = '退货单状态为不可以入库';
                    return false;
                }
                break;
            case 'CANCEL':
            case 'CLOSE':
                if ($return_status=='2' || $return_status=='3' || $return_status=='5'){

                    $msg = '退货单状态为不可以取消';
                    return false;
                }
                break;
        }
        return true;
    }

   
   private function _save_iostockorder($data){
        
        $op_name = kernel::single('desktop_user')->get_name();
        $iostock_instance = kernel::single('console_iostockorder');
        $shift_data = array();
        foreach($data['items'] as $item){
            $shift_data[$item['product_id']] = $item;
        }
        $shift_data = array (
                'iostockorder_name' => date('Ymd').'出库单',
                'supplier_id'       => $data['supplier_id'],
                'branch'            => $data['branch_id'],
                'type_id'           => '10',
                'iso_price'         => 0,
                'memo'              => $data['memo'],
                'operate_time'      => $data['operate_time'],
                'operator'          => $op_name,
                'products'          => $shift_data,
                'original_bn'       => $data['rp_bn'],
                'original_id'       => $data['rp_id'],
                'confirm'           => 'Y',
                'supplier'          =>$this->getSupplier($data['supplier_id']),
        );

        $result = $iostock_instance->save_iostockorder($shift_data, $msg);
        return $result;
    }
    

    /**
     * 参数校验
     * @param array $params 
     * @param string $msg 
     */
    private function checkParams($params,&$msg){
        return true;
    }

    /**
    * 冻结库存添加与释放
    * 
    */
    public function clear_stockout_store_freeze($data,$io_status=''){
        $basicMaterialStock    = kernel::single('material_basic_material_stock');
        $libBranchProduct    = kernel::single('ome_branch_product');
        $_items = $this->_returnpurchase['items'];
        $items = $data['items'];
       
        $branch_id = $data['branch_id'];
        $productIds = array();
        foreach($items as $item){
            if ($item['nums']<=0) continue;
            $effective_num = isset($item['effective_num']) ? $item['effective_num'] : ($item['nums']-$item['out_num']);
            $num = 0;
            if ($io_status == 'FINISH' || $io_status == ''){
                $num = $effective_num;
            }else{
                $num = $effective_num>0 ? $item['nums'] : $effective_num;
            }
            
            $product_id = $item['product_id'];
            

            if ($num>0){
                $basicMaterialStock->chg_product_store_freeze($product_id,$num,'-','return_purchase');
                $libBranchProduct->chg_product_store_freeze($branch_id,$product_id,$num,'-','return_purchase');
            }
            
        }
        
        //当状态为全部出库时需将未产生过出入库记录释放在途库存
        if ($io_status == 'FINISH'){
            $iostock_list = $this->getIostockList(10,$this->_returnpurchase['rp_id']);
            
            foreach ($_items as $_item){
                $num = 0;
                
                if($_item['out_num']==0 && !in_array($_item['product_id'],$iostock_list)){
                    $num = $_item['nums'];
                    
                }
                
                //处理之前部分出库
                if(($_item['out_num']>0 && $_item['nums']>$_item['out_num']) && in_array($_item['product_id'],$iostock_list) && (in_array($_item['product_id'],$productIds)==false)){

                    $num = $_item['nums']-$_item['out_num'];
                    
                }
               
                if ($num>0){
                    $basicMaterialStock->chg_product_store_freeze($_item['product_id'],$num,'-','return_purchase');
                    $libBranchProduct->chg_product_store_freeze($branch_id,$_item['product_id'],$num,'-','return_purchase');
                }
            }
        }
    }

    /**
    * 转换备注
    *
    */
     function format_memo($oldmemo,$newmemo){
        if ($newmemo){#有备注更新
            $memo = array();
            $operator       = kernel::single('desktop_user')->get_name();
            $operator = $operator=='' ? 'system' : $operator;
            if (!$oldmemo){
                $oldmemo= unserialize($oldmemo);
                if ($oldmemo)
                foreach($oldmemo as $k=>$v){
                    $memo[] = $v;
                }
            }
            $memo[]= array('op_name'=>$operator, 'op_time'=>date('Y-m-d H:i',time()), 'op_content'=>htmlspecialchars($newmemo));

            $memo = serialize($memo);
            return $memo;
        }
    }

   

    
    /**
     * 检查库存是否不足
     * @param   array    $items
     * @param   int      $branch_id
     * @return  string   $msg
     * @access  public
     */
    public function checkStore($branch_id,$items,&$msg)
    {
        $basicMaterialObj    = app::get('material')->model('basic_material');
        
        $libBranchProduct    = kernel::single('ome_branch_product');
        
        
        $error_msg = array();
        foreach($items as $item){
          
            $bn = $item['bn'];
            $products    = $basicMaterialObj->dump(array('material_bn'=>$bn), 'bm_id, material_bn');
            
            $store = $libBranchProduct->getStoreByBranch($products['bm_id'], $branch_id);
            
            if ($item['num'] > $store) {
                $error_msg[] = $bn.'库存不足';
            }
        }
        if (count($error_msg) > 0 ) {
            $msg = implode(',',$error_msg);
            return false;
        }
        return true;
    } // end func

}